﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Windows.Forms;

namespace BLL
{
    public enum LoaiQuyen
    {
        SinhVien,
        CoiPhongLab,
        BaoTriPhongLab,
        Admin
    }

    public class QLLoaiQuyen
    {
        public static string GetTenQuyen(LoaiQuyen lq)
        {
            string kq = "";
            switch (lq)
            {
                case LoaiQuyen.SinhVien:
                    kq = "Sinh Viên";
                    break;
                case LoaiQuyen.CoiPhongLab:
                    kq = "Coi Phòng Lab";
                    break;
                case LoaiQuyen.BaoTriPhongLab:
                    kq = "Bảo Trì Phòng Lab";
                    break;
                case LoaiQuyen.Admin:
                    kq = "Admin";
                    break;
                default:
                    break;
            }
            return kq;
        }
    }
   public class QLPhanQuyenSuDung
    {
        public static IEnumerable<PhanQuyenSuDung> GetAll()
        {
            return Generic.GetAll<PhanQuyenSuDung>();
        }
        public static ActionResult Insert(PhanQuyenSuDung pqsd)
        {
            ActionResult kq = new ActionResult();
            try
            {
                 Generic.Insert<PhanQuyenSuDung>(pqsd);
            }
            catch(Exception ex)
            {
                kq.Exceptions.Add(ex);
            }
            return kq;
        }
        public static int Delete(PhanQuyenSuDung tb)
        {
            return Generic.DeleteByCondition<PhanQuyenSuDung>(tb1 => tb1.MaQuyenSuDung == tb.MaQuyenSuDung&&tb1.MaTK==tb.MaTK);
        }
        
        public static IEnumerable<PhanQuyenSuDung> FindByCondition(Func<PhanQuyenSuDung, bool> func)
        {
            return Generic.FindByCondition<PhanQuyenSuDung>(func);
        }
        public static bool Any(Func<PhanQuyenSuDung, bool> func)
        {
            return Generic.FindByCondition<PhanQuyenSuDung>(func).ToList().Count > 0;
        }
        public static int DeleteByCondition(Func<PhanQuyenSuDung, bool> func)
        {
            return Generic.DeleteByCondition<PhanQuyenSuDung>(func);
        }
        public static ActionResult UpdatePhanQuyen(string mataikhoan, List<PhanQuyenSuDung> dspqsd)
        {
            ActionResult result = new ActionResult();
            try
            {
                DeleteByCondition(x => x.MaTK == mataikhoan);
                dspqsd.ForEach(x => x.MaTK = mataikhoan);
                dspqsd.ForEach(x =>result.Exceptions.AddRange(QLPhanQuyenSuDung.Insert(x).Exceptions));
            }
            catch (Exception ex)
            {
                result.Exceptions.Add(ex);
            }
            return result;
        }
        public static  ActionResultAsList<PhanQuyenSuDung> ThemQuyenSinhVien()
        {
            ActionResultAsList<PhanQuyenSuDung> results = new ActionResultAsList<PhanQuyenSuDung>();
            try{
                PhanQuyenSuDung pqsd_capnhattaikhoan=new PhanQuyenSuDung();
                pqsd_capnhattaikhoan.MaQuyenSuDung="CapNhatTKCaNhan";
                results.Result.Add(pqsd_capnhattaikhoan);

                PhanQuyenSuDung pqsd_guidexuat = new PhanQuyenSuDung();
                pqsd_guidexuat.MaQuyenSuDung = "GuiDX";
                results.Result.Add(pqsd_guidexuat);

                PhanQuyenSuDung pqsd_guiloitb = new PhanQuyenSuDung();
                pqsd_guiloitb.MaQuyenSuDung = "GuiLoiTB";
                results.Result.Add(pqsd_guiloitb);
            }
            catch(Exception ex)
            {
                results.Exceptions.Add(ex);
            }
            return results;
            
           
        }
        public static ActionResultAsList<PhanQuyenSuDung> ThemQuyenAdmin()
        {
            ActionResultAsList<PhanQuyenSuDung> results = new ActionResultAsList<PhanQuyenSuDung>();
            try
            {
                var dsqsd = QlQuyenSuDung.GetAll().ToList();
                foreach (var item in dsqsd)
                {
                    PhanQuyenSuDung pqsd = new PhanQuyenSuDung();
                    pqsd.MaQuyenSuDung = item.MaQuyenSuDung;
                    results.Result.Add(pqsd);
                }


            }
            catch (Exception ex)
            {
                results.Exceptions.Add(ex);
            }
            return results;


        }
        public static ActionResultAsList<PhanQuyenSuDung> ThemQuyenCoiPhongLab()
        {
            ActionResultAsList<PhanQuyenSuDung> results = new ActionResultAsList<PhanQuyenSuDung>();
            try
            {
                var dsquyensinhvien = ThemQuyenSinhVien().Result;
                results.Result.AddRange(dsquyensinhvien);

                PhanQuyenSuDung pqsd_themtbltb = new PhanQuyenSuDung();
                pqsd_themtbltb.MaQuyenSuDung = "ThemThongBaoLTB";
                results.Result.Add(pqsd_themtbltb);

                PhanQuyenSuDung pqsd_suatbltb = new PhanQuyenSuDung();
                pqsd_suatbltb.MaQuyenSuDung = "SuaThongBaoLTB";
                results.Result.Add(pqsd_suatbltb);

                PhanQuyenSuDung pqsd_xoatbltb = new PhanQuyenSuDung();
                pqsd_xoatbltb.MaQuyenSuDung = "XoaThongBaoLTB";
                results.Result.Add(pqsd_xoatbltb);

                PhanQuyenSuDung pqsd_intb = new PhanQuyenSuDung();
                pqsd_intb.MaQuyenSuDung = "InTB";
                results.Result.Add(pqsd_intb);

                PhanQuyenSuDung pqsd_InThongBaoLoiTB = new PhanQuyenSuDung();
                pqsd_InThongBaoLoiTB.MaQuyenSuDung = "InThongBaoLoiTB";
                results.Result.Add(pqsd_InThongBaoLoiTB);
            }
            catch (Exception ex)
            {
                results.Exceptions.Add(ex);
            }
            return results;


        }

        public static ActionResultAsList<PhanQuyenSuDung> ThemQuyenBaoTriPhongLab()
        {
            ActionResultAsList<PhanQuyenSuDung> results = new ActionResultAsList<PhanQuyenSuDung>();
            try
            {
                var dsquyencoiphonglab = ThemQuyenCoiPhongLab().Result;
                results.Result.AddRange(dsquyencoiphonglab);

                PhanQuyenSuDung pqsd_suatb = new PhanQuyenSuDung();
                pqsd_suatb.MaQuyenSuDung = "SuaTB";
                results.Result.Add(pqsd_suatb);

                PhanQuyenSuDung pqsd_themtb = new PhanQuyenSuDung();
                pqsd_themtb.MaQuyenSuDung = "ThemTB";
                results.Result.Add(pqsd_themtb);

                PhanQuyenSuDung pqsd_ThemKQBaoTriTB = new PhanQuyenSuDung();
                pqsd_ThemKQBaoTriTB.MaQuyenSuDung = "ThemKQBaoTriTB";
                results.Result.Add(pqsd_ThemKQBaoTriTB);

                PhanQuyenSuDung pqsd_SuaKQBaoTriTB = new PhanQuyenSuDung();
                pqsd_SuaKQBaoTriTB.MaQuyenSuDung = "SuaKQBaoTriTB";
                results.Result.Add(pqsd_SuaKQBaoTriTB);

                PhanQuyenSuDung pqsd_XoaKQBaoTriTB = new PhanQuyenSuDung();
                pqsd_XoaKQBaoTriTB.MaQuyenSuDung = "XoaKQBaoTriTB";
                results.Result.Add(pqsd_XoaKQBaoTriTB);

                PhanQuyenSuDung pqsd_InKQBaoTriTB = new PhanQuyenSuDung();
                pqsd_InKQBaoTriTB.MaQuyenSuDung = "InKQBaoTriTB";
                results.Result.Add(pqsd_InKQBaoTriTB);
            }
            catch (Exception ex)
            {
                results.Exceptions.Add(ex);
            }
            return results;


        }
        public static ActionResultAsList<PhanQuyenSuDung> ThemPhanQuyen(LoaiQuyen lq)
        {
            ActionResultAsList<PhanQuyenSuDung> result = new ActionResultAsList<PhanQuyenSuDung>();
            switch (lq)
            {
                case LoaiQuyen.SinhVien:
                    result = ThemQuyenSinhVien();
                    break;
                case LoaiQuyen.CoiPhongLab:
                    result= ThemQuyenCoiPhongLab();
                    break;
                case LoaiQuyen.BaoTriPhongLab:
                    result= ThemQuyenBaoTriPhongLab();
                    break;
                case LoaiQuyen.Admin:
                    result= ThemQuyenAdmin();
                    break;
                default:
                    break;
            }
            return result;
        }
   }
}
